package com.git_taoxian.test3;

import java.io.*;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.Callable;

public class MyCallable implements Callable {
    private Socket socket;

    public MyCallable() {
    }

    public MyCallable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public Object call() throws Exception {
        DataOutputStream dos = null;
        try {
//        获取字节输入输出流对象
            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();
            byte[] buf = new byte[1024];//服务器端每次读取的字节数据量
            int len;
//        获取当前时间的毫秒值
            Date date = new Date();
            long time = date.getTime();
            String name = time + "";
//        定义字节输入缓冲流对象
            File file = new File("d:/java/file");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file + name));
            File newFile = new File(file + name);
            while ((len = is.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
//        反馈消息给客户端
            if (newFile.exists()) {
                dos = new DataOutputStream(os);
                dos.writeUTF("成功！");
            } else {
                dos = new DataOutputStream(os);
                dos.writeUTF("失败！");
            }

//        关闭资源
            bos.close();
            is.close();
            os.close();
            socket.close();

        } catch (
                IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
